﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MVCOA.IDAL;
using MVCOA.Model;

namespace MVCOA.DAL
{
    internal class UserInfoDal:IUserInfoDal
    {
        OAEntities Db=new  OAEntities();
        public IQueryable<UserInfo> LoadENtity(Expression<Func<UserInfo, bool>> wExpressions)
        {
           return  Db.UserInfoes.Where(wExpressions);
        }

        public IQueryable<UserInfo> LoadPageEntities<TS>(int pageIndex, int pageSize, out int pageCount, Expression<Func<UserInfo, bool>> whereLamba,
            Expression<Func<UserInfo, TS>> orderlamba, bool isdes)
        {
            var temp = Db.UserInfoes.Where(whereLamba);
            pageCount = temp.Count();
            if (isdes)
            {
                temp = temp.OrderByDescending(orderlamba).Take(pageSize).Skip((pageIndex - 1)*pageSize);
            }
            else
            {
                temp = temp.OrderBy(orderlamba).Take(pageSize).Skip((pageIndex - 1) * pageSize);
            }
            return temp;
        }

        public bool DeleteEntity(UserInfo entity)
        {
          var temp=   Db.UserInfoes.Remove(entity); 
            return Db.SaveChanges()>0;
        }

        public bool UpdateEntity(UserInfo entity)
        {
            Db.Entry(entity).State=EntityState.Modified;
            return Db.SaveChanges() > 0;

        }

        public UserInfo AddEntity(UserInfo entity)
        {
            Db.UserInfoes.AddOrUpdate(entity);
            return entity;   
        }
    }
}
